<template>
  <el-dialog
    v-el-drag-dialog
    title="批量新增仓库"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="100px">
      <el-form-item label="关联类型:" prop="reType">
        <el-select v-model="temp.reType" clearable placeholder="请选择" style="width: 100%;" @change="selectRetype">
          <el-option :value="1<<1" label="门市" />
          <!-- <el-option :value="1<<2" label="配送员" /> -->
          <el-option :value="1<<3" label="气站" />
        </el-select>
      </el-form-item>
      <el-form-item label="关联机构:" prop="orgSNNoIDS">
        <el-tree-select
          v-model="temp.orgSNNoIDS"
          class="tree-select"
          :select-params="{
            clearable: false,
            multiple: true,
            placeholder: '请选择'
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgSNNo',
              label: 'orgName'
            },
            data: treeData
          }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import getTreeData from '@/utils/jsonTree'
  import { warehouseBatchAdd } from '@/api'

  export default {
    name: 'BatchAdd',
    props: {
      reTypeMap: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        treeData: [],
        temp: {
          reType: [],
          orgSNNoIDS: []
        },
        rules: {
          reType: [{ required: true, message: '请选择关联类型', trigger: 'change' }],
          orgSNNoIDS: [{ required: true, message: '请选择关联机构', trigger: 'change' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    methods: {
      open(temp) {
        this.getTreeData()
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      // 选择类型
      selectRetype(value) {
        console.log(value)
        let data = []
        if (value === 2) {
          data = this.orgList.filter(item => item.orgType === 11) // 门市
        } else {
          data = this.orgList.filter(item => item.orgType === 7) // 气站
        }
        console.log(data)
        this.treeData = getTreeData(data)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const reType = this.temp.reType
          // this.temp.reType.forEach(item => {
          //   reType |= Number(item)
          // })
          const params = {
            reType,
            orgSNNoIDS: this.temp.orgSNNoIDS
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await warehouseBatchAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '仓库信息保存成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          reType: [],
          orgSNNoIDS: []
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
